package com.sari.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by hsujim on 2016/8/27.
 */
public class PolygonArea {

    public static void main(String args[]){
        String p1 = "-2000,-50000";
        String p2 = "-1990,-50000";
        String p3 = "-1990,-49990";
        String p4 = "-2000,-49990";

        List list = new ArrayList();
        list.add(p1);
        list.add(p2);
        list.add(p3);
        list.add(p4);

        PolygonArea t = new PolygonArea();
        double area = t.getArea(list);
        System.out.println(area);
    }

    public static double getArea(List list)
    {
        //S = 0.5 * ( (x0*y1-x1*y0) + (x1*y2-x2*y1) + ... + (xn*y0-x0*yn) )

        double area = 0.00;
        Map map0 = (Map)list.get(0);
        for(int i = 0;i<list.size();i++){
            Map mapi = (Map)list.get(i);
            if(i<list.size()-1){
                Map mapi1 = (Map)list.get(i+1);
                Point p1 = new Point(mapi.get("GPS_LNGTD").toString()+","+mapi.get("GPS_LATITD").toString());
                Point p2 = new Point(mapi1.get("GPS_LNGTD").toString()+","+mapi1.get("GPS_LATITD").toString());
                area += p1.getX()*p2.getY() - p2.getX()*p1.getY();
            }else{
                Point pn = new Point(mapi.get("GPS_LNGTD").toString()+","+mapi.get("GPS_LATITD").toString());
                Point p0 = new Point(map0.get("GPS_LNGTD").toString()+","+map0.get("GPS_LATITD").toString());
                area += pn.getX()*p0.getY()- p0.getX()*pn.getY();
            }

        }
        area = area/2.00;

        return area;
    }


}

class Point{
    public double getX() {
        return x;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getY() {
        return y;
    }

    public void setY(double y) {
        this.y = y;
    }

    double x;
    double y;
    Point(String val){
        x = Double.parseDouble(val.split(",")[0]);
        y = Double.parseDouble(val.split(",")[1]);
    }

}
